//
//  ChangPokViewController.m
//  cherryproduct
//
//  Created by zou shigang on 14/9/13.
//  Copyright (c) 2014年 cherry. All rights reserved.
//

#import "ChangPokViewController.h"

@interface ChangPokViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView * tableview;

@end

@implementation ChangPokViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setTitle:@"消息提醒"];
    [self addNavBarButton:RIGHT_FINISH];
    
    if (self.select_index < 0) {
        self.select_index=0;
    }
    
    // Do any additional setup after loading the view.
    if (self.tableview == nil) {
        self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableview.userInteractionEnabled = YES;
        self.tableview.delegate = self;
        self.tableview.dataSource = self;
        self.tableview.showsVerticalScrollIndicator = NO;
        [self.myView addSubview:self.tableview];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)finishButtonAction:(UIButton *)sender
{
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] initWithCapacity:1];
    //dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:_textview.text,@"warning",nil];
    if (self.select_index==0) {
        [dic setObject:@"5" forKey:@"warning"];
    }
    if (self.select_index==1) {
        [dic setObject:@"10" forKey:@"warning"];
    }
    if (self.select_index==2) {
        [dic setObject:@"30" forKey:@"warning"];
    }
    if (self.select_index==3) {
        [dic setObject:@"60" forKey:@"warning"];
    }
    
    [self.view startLoadingAnimation];
    [[HttpNewBaseService sharedClient] postRequestOperation:@"member/set_profile"
                                            paramDictionary:dic
                                               onCompletion:^(NSString *responseString) {
                                                   [self.view stopLoadingAnimation];
                                                   
                                                   NSDictionary * dic = [responseString JSONValue];
                                                   if (dic) {
                                                       NSInteger status = [[dic objectForKey:@"status"] integerValue];
                                                       if (status==1) {
                                                           /*
                                                           UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"修改成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                                                           [alert show];*/
                                                           [self goBackPage];
                                                           
                                                           return;
                                                       }
                                                   }
                                                   [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
                                                   //strTitle = nil;
                                                   //tempLabel.text = _textview.text;
                                                   
                                               } onError:^(NSError *error) {
                                                   [self.view stopLoadingAnimation];
                                                   //strTitle = nil;
                                                   //tempLabel.text = _textview.text;
                                                   //[self.navigationController popViewControllerAnimated:YES];
                                                   [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
                                               } onAnimated:YES];
}

- (void)goBackPage
{
    NSString * str = @"5";
    if (self.select_index==1) {
        str = @"10";
    }
    if (self.select_index==2) {
        str = @"30";
    }
    if (self.select_index==3) {
        str = @"60";
    }
    
    if (self.block_dismiss) {
        self.block_dismiss(str);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString * str = @"5";
    if (self.select_index==1) {
        str = @"10";
    }
    if (self.select_index==2) {
        str = @"30";
    }
    if (self.select_index==3) {
        str = @"60";
    }
    
    if (self.block_dismiss) {
        self.block_dismiss(str);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark -
#pragma mark UITableViewDataSource UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString * str_cell = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str_cell];
    
    CGFloat width = self.view.frame.size.width;
    
    if (cell == nil) {
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str_cell];
        
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        UILabel * label_bb = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 200, 45)];
        label_bb.backgroundColor=[UIColor whiteColor];
        label_bb.font=[UIFont systemFontOfSize:14];
        label_bb.textColor=[UIColor grayColor];
        label_bb.tag=1;
        [cell addSubview:label_bb];
        
        UIImageView * image_view = [[UIImageView alloc] initWithFrame:CGRectMake(width-50, 13, 18, 18)];
        image_view.image=[UIImage imageNamed:@"ChangeNO"];
        image_view.tag=2;
        [cell addSubview:image_view];
        
        UIImageView * imageview_line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, width, 1)];
        imageview_line.backgroundColor=[UIColor grayColor];
        [cell addSubview:imageview_line];
    }
    UILabel * label = (UILabel *)[cell viewWithTag:1];
    UIImageView * image_view = (UIImageView *)[cell viewWithTag:2];
    
    NSInteger index = indexPath.row;
    label.text=@"";
    if (index == 0) {
        label.text=@"5分钟";
    }
    if (index == 1) {
        label.text=@"10分钟";
    }
    if (index == 2) {
        label.text=@"30分钟";
    }
    if (index == 3) {
        label.text=@"60分钟";
    }
    
    if (self.select_index == index) {
        image_view.image = [UIImage imageNamed:@"ChangeSelect"];
    } else {
        image_view.image = [UIImage imageNamed:@"ChangeNO"];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    self.select_index = indexPath.row;
    [self.tableview reloadData];
}

@end






























